
import time
import sys
sys.path.insert(0,'/home/mike/projects/jukebox')

import pygame.display
import pygame.font


pygame.font.init()


from jukebox.Album import Album
from jukebox.Widget import *


a = Album('/home/mike/sound/albums/bob-marley--uprising')

i = 0
for x in a.tracks:
    i = i + 1
    assert i == x.number()
    if i == 1:
        assert "coming in from the cold" == x.name()
        assert "/storage/sound/artists/bob-marley/uprising--coming-in-from-the-cold.mp3" == x.file()
    print x.number(), x.name(), x.file()

screen = pygame.display.set_mode((1024,768))

w = Widget(10,10,200,50)
w.draw(screen,0,0)
pygame.display.update()

w = AlbumCover(a)

font = pygame.font.Font('/usr/share/fonts/truetype/mike-downloaded/pinball.ttf',35)
font = pygame.font.Font('/usr/share/fonts/truetype/mike-downloaded/acknowledge-tt.ttf',35)
font = pygame.font.Font('/usr/share/fonts/truetype/mike-downloaded/telegraphic.ttf',35)
font = pygame.font.Font('/usr/share/fonts/truetype/mike-downloaded/tiquet.ttf',35)
font = pygame.font.Font('/usr/share/fonts/truetype/mike-downloaded/transistor.ttf',35)
font = pygame.font.Font('/usr/share/fonts/truetype/mike-downloaded/transponder.ttf',35)
font = pygame.font.Font('/usr/share/fonts/truetype/mike-downloaded/px10.ttf',35)

d = TextRadioButton( (10,700), "shuffle", font )
e = TextRadioButton( (10 + d.size[0] + 10,700), "sequential", font)
d.addToGroup(e)
d.clicked()

f = TextRadioButton( (e.size[0] + e.location[0] + 60,700), "album", font )
g = TextRadioButton( (f.size[0] + f.location[0] + 20,700), "track", font )
f.addToGroup(g)
f.clicked()

b = RadioButton( (10,10), pygame.image.load('media/mike-avatar.jpg'), pygame.image.load('media/foo.jpg'))
b.clicked()
c = RadioButton( (400,10), pygame.image.load('media/mike-avatar.jpg'), pygame.image.load('media/foo.jpg'))
b.addToGroup(c)

h = Label( (300,100), "I'm a label!", font)

widgets = [w,b,c,d,e,f,g,h]

#w.addBehavior(Resize( (300,300), 10 ))
w.addBehavior(Fly(800,500))

st = time.time()
frames = 0
done = False

dim = pygame.Surface( (1024,768))
dim.fill( (0,0,0) )
dim.set_alpha(128)

textinput = True
text = ''

cursoron = pygame.image.load('media/cursor-on.jpeg').convert()
cursoroff = pygame.image.load('media/cursor-off.jpeg').convert()

class Quit(Exception):
    pass

def processEvent(e):
    global text, textinput
    if e.type == pygame.NOEVENT:
        return
    
    elif e.type == pygame.QUIT:
        raise Quit()

    elif textinput and e.type == pygame.KEYUP:
        print e
        if e.key == 13:
            textinput = False
        elif e.key == 8:
            text = text[:-1]
        elif e.key > 13 and e.key < 256:
            text = text + chr(e.key).upper()

    if not textinput:
        for x in widgets:
            x.processEvent(e)
        

def drawroundyrect( surface, color, rect, width=1 ):
    pygame.draw.rect(surface, color, rect, width)

cursor = True
cursorcount = 0
inputlabel = font.render("INPUT: ", True, (0,255,0), (0,0,0))

try:
    while not done:
        screen.fill( (0,0,0) )
        for x in widgets:
            x.animate()
            x.draw(screen,0,0)

        if textinput:
            screen.blit(dim,(0,0))
            t = font.render(text, True, (0,200,0), (0,0,0))
            x = 200
            drawroundyrect(screen, (0,255,0), (x-inputlabel.get_size()[0]-10, 345, inputlabel.get_size()[0] + t.get_size()[0] + 5 + 5 + 10 + cursoron.get_size()[0], inputlabel.get_size()[1] + 5 + 5), 1)
            screen.blit(inputlabel, (x-inputlabel.get_size()[0],350))
            screen.blit(t, (x, 350))
            if cursor:
                screen.blit(cursoron, (x + t.get_size()[0] + 5, 350))
            else:
                screen.blit(cursoroff, (x + t.get_size()[0] + 5, 350))
            cursorcount = cursorcount + 1
            if cursorcount > 5:
                cursorcount = 0
                cursor = not cursor
            
        pygame.display.update()
        frames = frames + 1

        e = pygame.event.poll()
        if e.type != pygame.NOEVENT:
            processEvent(e)
            while pygame.event.peek((pygame.MOUSEMOTION,pygame.MOUSEBUTTONUP,pygame.MOUSEBUTTONDOWN)):
                processEvent(pygame.event.poll())


        #time.sleep(0.01)
except Quit:
    pass

diff = time.time() - st
print "diff:",diff
print "frames:",frames
print "frames / second:", (float(frames)/float(diff))
    
if 0:
    for x in range(0,50):
        screen.fill( (0,0,0) )
        w.draw(screen,-x,0)
        pygame.display.update()

